/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.event.retrievedefault;

import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.IFSManager;
import com.inspur.edp.bff.api.manager.context.RetrieveDefaultContext;
import com.inspur.edp.bff.entity.defaultvalue.VoDefaultValue;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.bff.spi.action.retrievedefault.AbstractRetrieveDefaultAction;
import com.inspur.edp.bff.spi.action.retrievedefault.AfterRetrieveDefaultAction;
import com.inspur.edp.bff.spi.action.retrievedefault.BeforeRetrieveDefaultAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.spi.entity.info.propertyinfo.DataTypePropertyInfo;

import java.util.ArrayList;
import java.util.List;

public class BffRetrieveDefaultEventBroker {
    private static List<IBffRetrieveDefaultEventListener> eventLs = new ArrayList<IBffRetrieveDefaultEventListener>() {{
    }};

    private static IBffRetrieveDefaultEventListener getBffEventListener(String className) {
        try {
            Class cls = Class.forName(className);
            IBffRetrieveDefaultEventListener listener = (IBffRetrieveDefaultEventListener) cls.newInstance();
            return listener;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2020);
        }

    }

    public static final void fireBeforeRetrieveDefault(IFSManager manager, VoDefaultValue defaultValue, String dataId) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeRetrieveDefault(manager, defaultValue, dataId);
        }
    }

    public static final void fireAfterRetrieveDefault(IFSManager manager, IEntityData data) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterRetrieveDefault(manager, data);
        }
    }

    public static final void fireExceptionStop(Exception e) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.exceptionStop(e);
        }
    }

    public static final void fireB4BeforeRetrieveDefaultAction(IFSManager manager, AbstractFSAction action, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeRetrieveDefaultAction(manager, action, retrieveDefaultContext);
        }
    }

    public static final void fireAfterBeforeRetrieveDefaultAction(IFSManager manager, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterRetrieveDefaultAction(manager, retrieveDefaultContext);
        }
    }

    public static final void fireB4ExecuteRetrieveDefaultActions(IFSManager manager, ArrayList<BeforeRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeExecuteRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireAfterExecuteRetrieveDefaultActions(IFSManager manager, ArrayList<BeforeRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterExecuteRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireB4AfterRetrieveDefaultAction(IFSManager manager, AbstractFSAction action, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeAfterRetrieveDefaultAction(manager, action, retrieveDefaultContext);
        }
    }

    public static final void fireAfterAfterRetrieveDefaultAction(IFSManager manager, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterAfterRetrieveDefaultAction(manager, retrieveDefaultContext);
        }
    }

    public static final void fireB4ExecuteAfterRetrieveDefaultActions(IFSManager manager, ArrayList<AfterRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeExecuteAfterRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireAfterExecuteAfterRetrieveDefaultActions(IFSManager manager, ArrayList<AfterRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterExecuteAfterRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireB4AbstractRetrieveDefaultAction(IFSManager manager, AbstractFSAction action, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeAbstractRetrieveDefaultAction(manager, action, retrieveDefaultContext);
        }
    }

    public static final void fireAfterAbstractRetrieveDefaultAction(IFSManager manager, RetrieveDefaultContext retrieveDefaultContext) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterAbstractRetrieveDefaultAction(manager, retrieveDefaultContext);
        }
    }

    public static final void fireB4ExecuteAbstractRetrieveDefaultActions(IFSManager manager, ArrayList<AbstractRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeExecuteAbstractRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireAfterExecuteAbstractRetrieveDefaultActions(IFSManager manager, ArrayList<AbstractRetrieveDefaultAction> actions) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterAbstractRetrieveDefaultActions(manager, actions);
        }
    }

    public static final void fireBeforeRetrieveDefaultChild(IFSManager manager, ArrayList<String> nodeCodes, ArrayList<String> lierachyIds, ArrayList<String> ids,
                                                            ArrayList<VoDefaultValue> defaultValues) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeRetrieveDefaultChild(manager, nodeCodes, lierachyIds, ids, defaultValues);
        }
    }

    public static final void fireAfterRetrieveDefaultChild(IFSManager manager, ArrayList<IEntityData> childData, ArrayList<String> nodeCodes) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterRetrieveDfaultChild(manager, childData, nodeCodes);
        }
    }

    public static final void fireBeforeDefaultValue(IFSManager manager, VoDefaultValue defaultValue) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeDefaultValue(manager, defaultValue);
        }
    }

    public static final void fireAfterDefaultValue(IFSManager manager, ArrayList<IEntityData> data) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterDefaultValue(manager, data);
        }
    }

    public static final void fireBeforeMetaDefaultValue(IFSManager manager, DataTypePropertyInfo propertyInfo) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.beforeMetaDefaultValue(manager, propertyInfo);
        }
    }

    public static final void firAfterMetaDefaultValue(IFSManager manager, ArrayList<IEntityData> data) {
        for (IBffRetrieveDefaultEventListener el : eventLs) {
            el.afterMetaDefaultValue(manager, data);
        }
    }

}
